package controller

import (
	"douyin/models"
	"douyin/repository/mysql"
	"douyin/service"
	"douyin/utils/pack"
	"strconv"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// Publish 视频投稿
func Publish(c *gin.Context) {
	//1.获取文件上传流
	data, err := c.FormFile("data")
	title := c.PostForm("title") //从form获取

	if err != nil {
		zap.L().Error("controller.Publish failed", zap.Error(err))
		pack.ResponseError(c, models.CodeGetVideoStreamError)
		return
	}
	userId, _ := c.Get(pack.CtxUserIDKey)
	// 2.业务处理
	err = service.Publish(data, userId.(int64), title)
	if err != nil {
		zap.L().Error("service.Publish failed", zap.Error(err))
		pack.ResponseError(c, models.CodeUploadVideoError)
		return
	}
	//3.返回响应
	pack.ResponseSuccess(c, models.CodeSuccess)
}

// PublishList 发布视频列表
func PublishList(c *gin.Context) {
	//1.调用业务处理
	userId, _ := c.Get(pack.CtxUserIDKey)                      //登录用户id
	otherId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64) //对方用户id

	videoList, err := service.PublishList(userId.(int64), otherId)
	//查询空处理
	if err == mysql.ErrorQueryZeroRecord {
		zap.L().Error("service.PublishList ErrorQueryZeroRecord!! ", zap.Error(err))
		models.ResponseVideoListSuccess(c, nil)
		return
	}
	if err != nil {
		zap.L().Error("service.PublishList failed", zap.Error(err))
		models.ResponseVideoListError(c, models.CodeGetVideoListError)
		return
	}
	//2.返回响应
	models.ResponseVideoListSuccess(c, videoList)
}
